package cn.lijian.goods.user.service;

import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;

import cn.itcast.commons.CommonUtils;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import cn.lijian.goods.user.dao.UserDao;
import cn.lijian.goods.user.domain.User;
import cn.lijian.goods.user.service.exception.UserException;

/**
 * �û�ģ�� ҵ���
 * @author lijian
 *
 */
public class UserService {

	private UserDao userDao=new UserDao();
	
	/**
	 * 修改密码
	 * @param uid
	 * @param newPass
	 * @param oldPass
	 * @author lijian
	 * @throws UserException 
	 */
	public void updatePassword(String uid,String newPass,String oldPass) throws UserException{
		try {
			boolean bool=userDao.findByUidAndPass(uid, oldPass);
			if(!bool){//旧的密码错误
				throw new UserException("旧的密码错误");
			}else{
				userDao.updatePassword(uid, newPass);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}
	
	/**
	 * 登录功能
	 * @param user
	 * @return
	 * @author lijian
	 */
	public User login(User user){
		try {
			return userDao.findByLoginnameAndLoginpass(user.getLoginname(), user.getLoginpass());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	/**
	 * 激活功能
	 * @param code
	 * @author lijian
	 * @throws UserException 
	 */
	public void activation(String code) throws UserException{
		//通过激活码查询用户
		User user;
		try {
			user = userDao.findByCode(code);
			//如果用户为空 说明是是无效激活码，抛出异常 ，无效激活码
			if(user==null){
				throw new UserException("无效的激活码");
			}
			//查看用户状态，如果 为 true ，抛出异常，请不要二次激活
			if(user.isStatus()){
				throw new UserException("您已经激活过了，不要二次激活");
			}
			//修改用户状态为 true
			userDao.updateStatus(user.getUid(), true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	public boolean ajaxValidateLoginname(String loginname){
		try {
			return userDao.ajaxValidateLoginname(loginname);
		} catch (SQLException e) {
			throw new RuntimeException(e); 
		}
	}
	
	public boolean ajaxValidateEmail(String email){
		try {
			return userDao.ajaxValidateEmail(email);
		} catch (SQLException e) {
			throw new RuntimeException(e); 
		}
	}
	/**
	 * 注册功能
	 * @param user
	 * @author lijian
	 */
	public void regist(User user){
		// 数据补齐
		user.setUid(CommonUtils.uuid());
		user.setStatus(true);
		user.setActivationCode(CommonUtils.uuid()+CommonUtils.uuid());
		// 向数据库插入
		try {
			userDao.add(user);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e); 
		}
		
		
		//发邮件
		//把配置文件加载到prop 中
		Properties prop=new Properties();
		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
		} catch (IOException e1) {
			throw new RuntimeException(e1); 
		}
		String host=prop.getProperty("host");//服务器主机名
		String name=prop.getProperty("username");//登录名
		String pwd=prop.getProperty("password");//密码
		Session session=MailUtils.createSession(host, name, pwd);
		//创建 Mail 对象
		String from=prop.getProperty("from");
		String to=user.getEmail();
		String subject=prop.getProperty("subject");
		//MessageFormat.format 方法会把第一个参数中的 {0}，使用第二个参数来替换，例如
		//MessageFormat.format.("你好{0}，你{1}","张三","吃了吗");--->你好，张三，你吃了吗
		String content=MessageFormat.format(prop.getProperty("content"),user.getActivationCode());
		Mail mail=new Mail(from,to,subject,content);
		//发送邮件
//		try {
////			MailUtils.send(session, mail);
//		} catch (MessagingException e) {
//			throw new RuntimeException(e); 
//		} catch (IOException e) {
//			throw new RuntimeException(e); 
//		}
		
	}
}
